Ext
		.define(
				"HRMobile.controller.LoginController",
				{
					extend : "Ext.app.Controller",
					config : {
						refs : {
							btnLogin : "#btnLogin",
							userName : "#txtUserName",
							password : "#txtPassword",
						},
						control : {
							btnLogin : {
								tap : "doAuthentication"
							}
						},
						stores : [ 'LeaveRequestStore', 'SurveyStore',
								'YourLeaveRequestStore', 'MySupervisorStore' ],
						models : [ 'LeaveRequest', 'Survey' ]
					},
					doAuthentication : function() {
						var username = this.getUserName().getValue();
						var password = this.getPassword().getValue();
						Ext.Ajax
								.request({
									url : 'Login',
									params : {
										username : username,
										password : password
									},
									failure : function(response) {
										if (response.timedout) {
											Ext.Msg.alert('Timeout',
													"The server timed out");
										} else if (response.aborted) {
											Ext.Msg.alert('Aborted',
													"Request aborted");
										} else {
											Ext.Msg
													.alert('Error',
															"Cannot connect to remote server");
										}
									},
									success : function(response) {
										var result = Ext.JSON
												.decode(response.responseText);
										if (result.isValid == true) {
											localStorage.userShortName = result.userShortName;
											localStorage.userId = result.userId;
											localStorage.userSubUnitId = result.userSubUnitId;
											localStorage.userIdSurvey = result.userIdSurvey;
											localStorage.canViewEmployeeList = result.canViewEmployeeList;
											localStorage.canViewChartResource = result.canViewChartResource;
											localStorage.canViewChartLeave = result.canViewChartLeave;

											Ext.getCmp('login').destroy();
											Ext.Viewport
													.setActiveItem(Ext
															.create('HRMobile.view.Main'));

											Ext
													.getCmp('lblWelcome')
													.setHtml(
															'<b>Welcome '
																	+ result.userShortName
																	+ '!</b>');

											var store = Ext
													.getStore('LeaveRequestStore');
											store.getProxy()
													.setExtraParam(
															'sup_number',
															result.userId);
											store.load();

											var yourLeaveStore = Ext
													.getStore('YourLeaveRequestStore');
											yourLeaveStore.getProxy()
													.setExtraParam('userId',
															result.userId);
											yourLeaveStore.load();

											var store = Ext
													.getStore('MySupervisorStore');
											store.getProxy()
													.setExtraParam(
															'emp_number',
															result.userId);
											store.load();

											var surveyStore = Ext
													.getStore('SurveyStore');
											surveyStore
													.getProxy()
													.setExtraParam(
															'userSubUnitId',
															result.userSubUnitId);
											surveyStore
													.getProxy()
													.setExtraParam('userId',
															result.userIdSurvey);
											surveyStore.load();

											if (result.canViewEmployeeList == false)
												Ext.getCmp('main').getTabBar().items
														.getAt(1).hide();

											if (result.canViewChartResource == false) {
												Ext.getCmp('crsHomePanel')
														.removeInnerAt(0);
												Ext.getCmp('crsHomePanel')
														.removeInnerAt(0);
												Ext.getCmp('crsHomePanel')
														.removeInnerAt(0);
												Ext
														.getCmp('slfReport')
														.setOptions(
																{
																	text : 'Leave taken by reason',
																	value : 0
																},
																{
																	text : 'Leave taken by month',
																	value : 1
																},
																{
																	text : 'Leave taken by subunit',
																	value : 2
																});
											}
											if (result.canViewChartLeave == false) {
												if (localStorage.canViewChartResource == 'false') {
													Ext.getCmp('crsHomePanel')
															.removeInnerAt(0);
													Ext.getCmp('crsHomePanel')
															.removeInnerAt(0);
													Ext.getCmp('crsHomePanel')
															.removeInnerAt(0);
													Ext.getCmp('slfReport')
															.setOptions({
																text : '',
																value : 0
															});
													Ext.getCmp('slfReport')
															.setDisabled(true);
												} else {
													Ext.getCmp('crsHomePanel')
															.removeInnerAt(3);
													Ext.getCmp('crsHomePanel')
															.removeInnerAt(3);
													Ext.getCmp('crsHomePanel')
															.removeInnerAt(3);
													Ext
															.getCmp('slfReport')
															.setOptions(
																	{
																		text : 'Employee by age',
																		value : 0
																	},
																	{
																		text : 'Employee by gender',
																		value : 1
																	},
																	{
																		text : 'Structure distribution',
																		value : 2
																	});
												}

											}
										} else {
											Ext.Msg.alert('Login failed',
													"Incorrect credential");
										}
									},
								});
					}

				// init and launch functions omitted.
				});
